package cinema.ELTEcinema;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import cinema.ELTEcinema_ejb.Dao.MovieDAO;
import cinema.ELTEcinema_ejb.Dao.RoomDAO;
import cinema.ELTEcinema_ejb.Dao.TimeTableDAO;
import cinema.ELTEcinema_ejb.Entity.Movie;
import cinema.ELTEcinema_ejb.Entity.Room;
import cinema.ELTEcinema_ejb.Entity.TimeTable;

@SuppressWarnings("serial")
@Named
@SessionScoped
public class AdminController implements Serializable{

	@Inject
	private MovieDAO movieDAO;
	@Inject
	private RoomDAO roomDAO;
	@Inject
	private TimeTableDAO timeTableDAO;
	@Inject
	LoginBean loginBean;

	private Date startTime;
	private String title;
	private int length;
	private String description;
	private Movie movie;
	private boolean showShowings;
	private boolean showFilms;
	private boolean showRooms;

	private int roomId;
	private int movieId;

	private TimeTable timeTable;
	
	private List<TimeTable> timeTables;
	private List<Room> rooms;
	private List<Movie> movies;

	@PostConstruct
	public void init(){
		showFilms = true;
		timeTables = timeTableDAO.findAll();
		rooms = roomDAO.findAll();
		movies = movieDAO.findAll();
	}

	public void checkLogin(){
		if(loginBean.getActiveUser() == null){
			((ConfigurableNavigationHandler)FacesContext.getCurrentInstance().getApplication().getNavigationHandler()).performNavigation("adminLogin");
		}
	}
	
	public String logOut(){
		loginBean.logOut();
		return "index?faces-redirect=true";
	}
	
	public List<Movie> getMovies() {
		return movies;
	}
	
	public void addTimeTable() {		 
		if(startTime!=null){
			TimeTable timeTable = new TimeTable(movieDAO.findById(movieId),roomDAO.findById(roomId),startTime);
			timeTables.add(timeTable);
			timeTableDAO.save(timeTable);
		}
	}

	public void deleteTimeTable() {
		timeTableDAO.delete(timeTable);
		timeTables.remove(timeTable);	
	}

	public void editTimeTable(){
		timeTable.setCanEdit(true);
	}

	public void saveTimeTable(){
		timeTable.setCanEdit(false);
		timeTableDAO.update(timeTable);
	}

	public String showFilms(){
		showShowings = false;
		showFilms = true;
		showRooms = false;
		return "admin.xhtml";
	}
	
	public String showShowings(){
		showShowings = true;
		showFilms = false;
		showRooms = false;
		return "admin.xhtml";
	}
	
	public String showRooms(){
		showShowings = false;
		showFilms = false;
		showRooms = true;
		return "admin.xhtml";
	}
	
	public void addMovie() {		 
		Movie movie = new Movie(title,length,description);
		movies.add(movie);
		movieDAO.save(movie);
	}

	public void deleteMovie() {
		try{
			movieDAO.delete(movie);
			movies.remove(movie);
		}
		catch(Exception e){
			FacesContext.getCurrentInstance().addMessage("movieTable",
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "cannot delete movie if showing is registered for it", "cannot delete movie if showing is registered for it"));
		}
	}

	public void editMovie(){
		movie.setCanEdit(true);
	}

	public void saveMovie(){
		movie.setCanEdit(false);
		movieDAO.update(movie);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Movie getMovie() {
		return movie;
	}

	public void setMovie(Movie movie) {
		this.movie = movie;
	}

	public boolean isShowShowings() {
		return showShowings;
	}

	public void setShowShowings(boolean showShowings) {
		this.showShowings = showShowings;
	}

	public boolean isShowFilms() {
		return showFilms;
	}

	public void setShowFilms(boolean showFilms) {
		this.showFilms = showFilms;
	}

	public boolean isShowRooms() {
		return showRooms;
	}

	public void setShowRooms(boolean showRooms) {
		this.showRooms = showRooms;
	}
	
	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public int getRoomId() {
		return roomId;
	}

	public void setRoomId(int roomId) {
		this.roomId = roomId;
	}

	public int getMovieId() {
		return movieId;
	}

	public void setMovieId(int movieId) {
		this.movieId = movieId;
	}

	public TimeTable getTimeTable() {
		return timeTable;
	}

	public void setTimeTable(TimeTable timeTable) {
		this.timeTable = timeTable;
	}

	public List<TimeTable> getTimeTables() {
		return timeTables;
	}

	public void setTimeTables(List<TimeTable> timeTables) {
		this.timeTables = timeTables;
	}

	public List<Room> getRooms() {
		return rooms;
	}

	public void setRooms(List<Room> rooms) {
		this.rooms = rooms;
	}

	public void setMovies(List<Movie> movies) {
		this.movies = movies;
	}
}
